#include "../EngineSource/EngineDef.h"
#include "Game.h"

#include <windows.h>
#include <GL\gl.h>
#include <GL\glut.h>
#include <stdio.h>

bool init = true;

Obj3DS *obj;

void Init()
{
	obj = new Obj3DS();
	obj->Load("box.3ds");
//	obj->Load("bldng119.3ds");

}

bool printVal = true;

vector<Vector3<float>> vertexList;

void Vertex(float x, float y, float z)
{
	if(printVal)
	{
		printf("x = %f y = %f z = %f \n", x, y, z);

		vertexList.push_back(Vector3<float>(x,y,z));
	}

		glVertex3f(	x,
					y,
					z);
}


void DrawNet(GLfloat size, GLint LinesX, GLint LinesZ)
{
	glBegin(GL_LINES);
	for (int xc = 0; xc < LinesX; xc++)
	{
		Vertex(	-size / 2.0f + xc / (GLfloat)(LinesX-1)*size,
					0.0,
					size / 2.0);
		Vertex(	-size / 2.0f + xc / (GLfloat)(LinesX-1)*size,
					0.0,
					size / -2.0f);
	}
	for (int zc = 0; zc < LinesX; zc++)
	{
		Vertex(	size / 2.0f,
					0.0,
					-size / 2.0f + zc / (GLfloat)(LinesZ-1)*size);
		Vertex(	size / -2.0f,
					0.0,
					-size / 2.0f + zc / (GLfloat)(LinesZ-1)*size);
	}
	glEnd();
}


void Game::RenderScene1()
{
	if(init)
	{
		init = false;
		Init();

		return ;
	}

	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
 
	Engine()->pCamera->Update();

	glBegin(GL_TRIANGLE_STRIP);

	for(int i = 0; i < obj->nMesh; i ++)
	{
		for(int j = 0; j < obj->Mesh[i].nFace; j ++)
		{
			int p1 = obj->Mesh[i].face[j].p1;
			Vertex( obj->Mesh[i].vtx[p1].x, obj->Mesh[i].vtx[p1].y, obj->Mesh[i].vtx[p1].z);
			int p2 = obj->Mesh[i].face[j].p2;
			Vertex( obj->Mesh[i].vtx[p2].x, obj->Mesh[i].vtx[p2].y, obj->Mesh[i].vtx[p2].z);
			int p3 = obj->Mesh[i].face[j].p3;
			Vertex( obj->Mesh[i].vtx[p3].x, obj->Mesh[i].vtx[p3].y, obj->Mesh[i].vtx[p3].z);
		}
	}

	glEnd();


	glFlush();  
	glutSwapBuffers();

	printVal = false;

	return ;

	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
 
	Engine()->pCamera->Update();
	glTranslatef(0.0f,0.8f,0.0f);

	glScalef(1.0f,1.0f,1.0f);
	
	GLfloat size = 1.0f;
	GLint LinesX = 2;
	GLint LinesZ = 2;
	
	GLfloat halfsize = size / 2.0f;
	glColor3f(1.0f,1.0f,1.0f);
	glPushMatrix();
		glTranslatef(0.0,-halfsize ,0.0);
		DrawNet(size,LinesX,LinesZ);
		glTranslatef(0.0,size,0.0);
		DrawNet(size,LinesX,LinesZ);
	glPopMatrix();
	glColor3f(0.0,0.0,1.0);
	glPushMatrix();
		glTranslatef(-halfsize,0.0,0.0);	
		glRotatef(90.0,0.0,0.0,halfsize);
		DrawNet(size,LinesX,LinesZ);
		glTranslatef(0.0,-size,0.0);
		DrawNet(size,LinesX,LinesZ);
	glPopMatrix();
	glColor3f(1.0f,0.0,0.0);
	glPushMatrix();
		glTranslatef(0.0,0.0,-halfsize);	
		glRotatef(90.0,halfsize,0.0,0.0);
		DrawNet(size,LinesX,LinesZ);
		glTranslatef(0.0,size,0.0);
		DrawNet(size,LinesX,LinesZ);
	glPopMatrix();
		
	glFlush();  
	glutSwapBuffers();

	
}